import Axios from 'axios'

const Cnode = {
  baseUrl: 'https://cnodejs.org/api/v1'
}

Axios.interceptors.request.use(config => {
  console.log('[http]', config.method, config.url, config.params)
  return config
})

Cnode.getPostList = function (params = {}) {
  return Axios.get(Cnode.baseUrl + '/topics', {
    params: params
  })
}

Cnode.getPostDetail = function (params = {}) {
  const postId = params.postId
  delete params.postId
  return Axios.get(Cnode.baseUrl + '/topic/' + postId, {
    params: params
  })
}

Cnode.publishPost = function (params = {}) {
  return Axios.post(Cnode.baseUrl + '/topics/update', {
    ...params
  })
}

Cnode.updatePost = function (params = {}) {
  return Axios.post(Cnode.baseUrl + '/topics', {
    ...params
  })
}

Cnode.collectPost = function (params = {}) {
  return Axios.post(Cnode.baseUrl + '/topic_collect/collect', {
    ...params
  })
}

Cnode.delCollectPost = function (params = {}) {
  return Axios.post(Cnode.baseUrl + '/topic_collect/de_collect', {
    ...params
  })
}

Cnode.collects = function (params = {}) {
  return Axios.get(Cnode.baseUrl + '/topic_collect/' + params.username)
}

Cnode.replies = function (params = {}) {
  return Axios.post(Cnode.baseUrl + `/topic/${params.postId}/replies`, {
    ...params
  })
}

Cnode.ups = function (params = {}) {
  return Axios.post(Cnode.baseUrl + `/reply/${replyId}/ups`, {
    ...params
  })
}

export default Cnode
